'use strict'

const Joi = require('joi')
const { BaseSvgScrapingService } = require('..')

const schema = Joi.object({
  message: Joi.alternatives()
    .try([/^\d*$/, Joi.equal('unknown')])
    .required(),
}).required()

module.exports = class SnykVulnerabilityBase extends BaseSvgScrapingService {
  static render({ vulnerabilities }) {
    let color = 'red'
    if (vulnerabilities === '0') {
      color = 'brightgreen'
    }
    return {
      message: vulnerabilities,
      color,
    }
  }

  async fetch({ url, qs, errorMessages }) {
    const { message: vulnerabilities } = await this._requestSvg({
      url,
      schema,
      options: {
        qs,
      },
      errorMessages,
    })

    return { vulnerabilities }
  }

  static get category() {
    return 'analysis'
  }

  static get defaultBadgeData() {
    return {
      label: 'vulnerabilities',
    }
  }
}
